package en.edu.zucc.pb.loginapplication.viewmodel;

import android.app.Application;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;

import java.util.ArrayList;
import java.util.List;

//import cn.chenjianlink.android.alarmclock.model.AlarmClock;
//import cn.chenjianlink.android.alarmclock.model.Messager;
//import cn.chenjianlink.android.alarmclock.repository.AlarmClockRepository;
//import cn.chenjianlink.android.alarmclock.utils.LogUtil;
//import cn.chenjianlink.android.alarmclock.utils.TimeUtil;
import en.edu.zucc.pb.loginapplication.entity.User;
import en.edu.zucc.pb.loginapplication.repository.UserRepository;

/**
 * @author chenjian
 * Activity对应的ViewModel
 */
public class UserViewModel extends AndroidViewModel {

    private final UserRepository repository=null;

    private final LiveData<List<User>> userList=null  ;

    /**
     * 用于临时存放响铃周期的列表
     */
    private List<Integer> ringRepeatList;

    /**
     * 用于临时存放铃声uri
     */
    private String ringMusicUri;

    public UserViewModel(@NonNull Application application) {
        super(application);
//        repository = new UserRepository(application);
//        userList = repository.getAll();
//        ringRepeatList = new ArrayList<>();
    }

    /**
     * 获取闹钟列表
     *
     * @return LiveData包装的闹钟列表
     */
    public LiveData<List<User>> getUserList() {
        Log.i("ViewModel.getAlarmClockList", userList.toString());
        return userList;
    }


//    public   List<User> findUserByUsername(String  username) {
//        //查找得到id对应的AlarmClock对象
//        // 是不是放在view model 里的就算是后台的 不算是ui 线程啊
//        // 不然这里返回的 为什么那边能收到
//        List<User> byUsername = repository.getByUsername(username);
//        return byUsername;
//
//    }

    /**
     * 清除临时数据
     */
    public void clearCache() {
        ringRepeatList.clear();
        ringMusicUri = null;
    }

    /**
     * 将List形式的响铃信息封装成int类型
     *
     * @return 封装信息
     */
    private int changeRingRepeatListToInt() {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < 7; i++) {
            if (ringRepeatList.contains(i + 1)) {
                builder.append(1);
            } else {
                builder.append(0);
            }
        }
        return Integer.parseUnsignedInt(builder.toString(), 2);
    }

    /**
     * 更新闹钟是否启用的信息
     *
     * @param user 要更新的闹钟对象
     */
    public void update(User user) {
        repository.update(user);
    }

    /**
     * 移除闹钟
     *
     * @param user 要移除的闹钟对象
     */
    public void delete(User user) {
        repository.delete(user);
    }

    /**
     * getter setter 方法
     * *********************************************************************************
     */
    public List<Integer> getRingRepeatList() {
        return ringRepeatList;
    }

    public void setRingMusicUri(String ringMusicUri) {
        this.ringMusicUri = ringMusicUri;
    }

    public String getRingMusicUri() {
        return ringMusicUri;
    }

}
